package com.cgc.venus.server.ci.utils;
import com.cgc.venus.server.ci.model.dto.PipelineJobDTO;
import com.cgc.venus.server.ci.model.dto.PipelineStageDTO;
import com.cgc.venus.server.ci.model.dto.StringParmDTO;
import com.cgc.venus.server.ci.model.entity.CiPipeline;
import com.cgc.venus.server.ci.model.vo.PipelineVO;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 业哥 on 2018/11/13.
 */
public class XmlUtil {

    public static String pipelineJobToXml(PipelineJobDTO pipelineJob) throws Exception {
        String xml;
        try {
            InputStream jobIs = XmlUtil.class.getResourceAsStream("/jobTemplate/pipelineJob.xml");
            BufferedReader in = new BufferedReader(new InputStreamReader(jobIs));
            StringBuffer buffer = new StringBuffer();
            String line = "";
            while ((line = in.readLine()) != null){
                buffer.append(line);
            }
            xml=buffer.toString();
            List<String> params=new ArrayList<String>();
            params.add(pipelineJob.getDesc());
            params.add(Long.toString(pipelineJob.getKeepBuildDay()));
            params.add(Long.toString(pipelineJob.getKeepBuildNumber()));
            List<StringParmDTO> stringParms=pipelineJob.getStringParms();
            StringBuffer sb=new StringBuffer("");
            if(stringParms!=null&&stringParms.size()>0){
                sb.append("<hudson.model.ParametersDefinitionProperty>");
                sb.append("<parameterDefinitions>");
                for(StringParmDTO sp:stringParms){
                    sb.append("<hudson.model.StringParameterDefinition>");
                    sb.append("<name>"+sp.getName()+"</name>");
                    sb.append("<description>"+sp.getDescription()+"</description>");
                    sb.append("<defaultValue>"+sp.getDefaultValue()+"</defaultValue>");
                    sb.append("</hudson.model.StringParameterDefinition>");
                }
                sb.append("</parameterDefinitions>");
                sb.append("</hudson.model.ParametersDefinitionProperty>");
            }

            params.add(sb.toString());
            StringBuffer sb1=new StringBuffer("");
            List<PipelineStageDTO> pipelineStages=pipelineJob.getPipelineStages();
            sb1.append("\npipeline {\n");
            sb1.append(" agent any \n");
            sb1.append(" stages {\n");
            Map<String,String> mapStage=new HashMap<String, String>();
            if(pipelineStages==null||pipelineStages.size()==0){
                throw new Exception("至少需要1个stage!");
            }
            for(PipelineStageDTO ps:pipelineStages){
                if(mapStage.containsKey(ps.getStageName())){
                    throw new Exception("Duplicate stage name: ["+ps.getStageName()+"]");
                }
                mapStage.put(ps.getStageName(),ps.getStageName());
                sb1.append("  stage(&apos;"+ps.getStageName()+"&apos;) {\n");
                sb1.append("   steps {\n");
                if(ps.getShellCmd()==null||ps.getShellCmd().size()==0){
                    throw new Exception("stage["+ps.getStageName()+"]至少需要1个shellCmd!");
                }
                for(String sh:ps.getShellCmd()){
                    sb1.append("    sh &apos;&apos;&apos;\n");
                    sb1.append("     "+sh+"\n");
                    sb1.append("    &apos;&apos;&apos;\n");
                }
                sb1.append("   }\n");
                sb1.append("  }\n");
            }
            sb1.append(" }\n");
            sb1.append("}");
            params.add(sb1.toString());
            xml=MessageFormat.format(xml,params.toArray());
        }catch (Exception ex){
            throw ex;
        }
        return xml;
    }
}
